package com.staginfo.segs.util;

import android.util.Log;

import com.staginfo.segs.common.Config;
import com.staginfo.segs.common.Constants;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by linton on 2017/3/7.
 */

public class Logs {

    private static final int MAX_SIZE = 200;
    private static List<String> logLists = new LinkedList<>();
    private static boolean mRecord = false;
    private static boolean isDebug = Config.isDebugModel;


    private Logs() {
    }

    public static boolean isRecording() {
        return mRecord;
    }

    public static void startRecord() {
        mRecord = true;
    }

    public static void stopRecord() {
        mRecord = false;
    }


    private static void keepCapacity() {
        while (logLists.size() >= MAX_SIZE) {
            logLists.remove(0);
        }
    }

    private static synchronized void addToCache(String string) {
        keepCapacity();
        try {
            logLists.add(DateUtils.dateToString(new Date(), DateUtils.HH_mm_ss_SSS) + " " + string+"\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void clearCache() {
        logLists.clear();
    }


    public static void v(String tag, String msg) {
        printAndAddToCache(Log.VERBOSE, tag, msg);
    }

    public static void d(String tag, String msg) {
        printAndAddToCache(Log.DEBUG, tag, msg);
    }

    public static void i(String tag, String msg) {
        printAndAddToCache(Log.INFO, tag, msg);
    }

    public static void w(String tag, String msg) {
        printAndAddToCache(Log.WARN, tag, msg);
    }

    public static void e(String tag, String msg) {
        printAndAddToCache(Log.ERROR, tag, msg);
    }

    public static void a(String tag, String msg) {
        printAndAddToCache(Log.ASSERT, tag, msg);
    }

    private static void printAndAddToCache(int priority, String tag, String msg) {
        if (isDebug) {
            Log.println(priority, tag, msg);
        }
        if (mRecord) {
            addToCache(tag + " " + msg);
        }
    }

    private static String listToString() {
        StringBuffer bufer = new StringBuffer();
        for (String logTxt :
                logLists) {
            bufer.append(logTxt).append("\n");
        }
        String bufferString = bufer.toString();
        return bufferString;
    }

    private static void writeCacheToFile() {
        try {
            String fileName = DateUtils.dateToString(new Date(), DateUtils.yyyyMMDD) + "@" + Constants.LOG_SUFFIX;
            File file = new File(SDCardUtils.getAbsolutePath(Constants.DEBUG_LOG_FILE_PATH + fileName));
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter filerWriter = new FileWriter(file, true);// 后面这个参数代表是不是要接上文件中原来的数据，不进行覆盖
            BufferedWriter bufWriter = new BufferedWriter(filerWriter);
            String logsText = listToString();
            bufWriter.write(logsText);
            bufWriter.newLine();
            bufWriter.close();
            filerWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public interface OnCompleteListener {
        void onComplete(boolean isSuccess);
    }
}
